/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.gephi.statistics.spi.path;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import u.can.i.up.db.postgresql.Aliases;

/**
 *
 * @author lczgywzyy
 */
public class LGraph implements Cloneable {

	// private LVertex vertexList[]; // list of vertices
	private HashMap<Integer, LVertex> vertexList = new HashMap<Integer, LVertex>();
	// private int adjMat[][]; // adjacency matrix

	// private int nVerts;
	private static int MAX_VERTS = -1; // n个点

	int i = 0;
	int j = 0;

	public HashMap<Integer, LVertex> getVertexList() {
		return vertexList;
	}

	public HashMap<Integer, HashMap<Integer, Integer>> getAdjMat() {
		// return adjMat;
		return LSparse_Matrix.getInstance().getMatrix();
	}

	public int getN() {
		return MAX_VERTS;
	}

	public LGraph(List<Aliases> aliases, HashSet<Integer> allNodeIDSet, int index) {
		MAX_VERTS = allNodeIDSet.size();// hgraph.getNodeCount();

		for (Aliases e : aliases) {
			addEdge(e.getSid(), e.getTid());
		}
		for (int i : allNodeIDSet) {
			addVertex(i);
		}

		switch (index) {
		case 0:
			break;
		case 1:
			delEdge(4, 2);
			break;
		default:
			break;
		}
	}

	public LGraph(int index) {
		MAX_VERTS = 8;
        addVertex(0);
        addVertex(1);
        addVertex(2);
        addVertex(3);
        addVertex(4);
        addVertex(5);
        addVertex(7);
        
        addEdge(0, 1);
        addEdge(0, 2);
        addEdge(1, 4);
        addEdge(2, 0);
        addEdge(2, 5);
        addEdge(3, 0);
        addEdge(3, 2);
        addEdge(3, 3);
        addEdge(4, 1);
        addEdge(4, 2);
        addEdge(5, 7);
        addEdge(7, 3);

		switch (index) {
		case 0:
			break;
		case 1:
			delEdge(4, 2);
			break;
		default:
			break;
		}
	}

	private void delEdge(int start, int end) {
		// adjMat[start][end] = 0;
		LSparse_Matrix.getInstance().setValue(start, end, 0);
	}

	private void addEdge(int start, int end) {// 有向图，添加边
		// adjMat[start][end] = 1;
		LSparse_Matrix.getInstance().setValue(start, end, 1);
	}

	public void addVertex(int lab) {
		// vertexList[nVerts++] = new LVertex(lab);// 添加点
		vertexList.put(lab, new LVertex("" + lab));
	}

	public String displayVertex(int i) {
		// return vertexList[i].getLabel();
		return vertexList.get(i).getLabel();
	}

	public boolean displayVertexVisited(int i) {
		// return vertexList[i].WasVisited();
		return vertexList.get(i).WasVisited();
	}

	public void printGraph() {
		// for (i = 0; i < MAX_VERTS; i++) {
		// System.out.print("第" + displayVertex(i) + "个节点:" + " ");
		//
		// for (j = 0; j < MAX_VERTS; j++) {
		// System.out.print(displayVertex(i) + "-" + displayVertex(j) + ": " +
		// LSparse_Matrix.getInstance().getValue(i, j) + " ");
		//// + "：" + adjMat[i][j] + " ");
		// }
		// System.out.println();
		// }

	}

	@SuppressWarnings("unchecked")
	@Override
	public Object clone() {
		LGraph lgraph = null;
		try{  
			lgraph = (LGraph)super.clone();   //浅复制  
        }catch(CloneNotSupportedException e) {  
            e.printStackTrace();  
        }  
		lgraph.vertexList = (HashMap<Integer, LVertex>) lgraph.vertexList.clone();
//        stu.addr = (Address)addr.clone();   //深度复制  
        return lgraph;  
	}
}
